﻿/**
 * jp.nium Classes
 * 
 * @author Copyright (C) 2007-2010 taka:nium.jp, All Rights Reserved.
 * @version 4.0.22
 * @see http://classes.nium.jp/
 * 
 * jp.nium Classes is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
package phpas3 {


	public function print_r( target:Object, return_val:Boolean=false ):* {
		var str:String = ObjectUtil.toString( target );
		if( return_val ){
			return str;
		} else {
			trace( str );
		}
	}
}



class ObjectUtil{
	/**
	 * <span lang="ja">指定されたオブジェクトのストリング表現を返します。</span>
	 * <span lang="en">Returns the string expression of the specified object.</span>
	 * 
	 * @param target
	 * <span lang="ja">対象のオブジェクトです。</span>
	 * <span lang="en">The object to get the string expression.</span>
	 * @return
	 * <span lang="ja">オブジェクトのストリング表現です。</span>
	 * <span lang="en">The string expression of the object.</span>
	 * 
	 * @example <listing version="3.0">
	 * </listing>
	 */
	public static function toString( target:Object ):String {
		//if ( target is Array ) { return ArrayUtil.toString( target as Array ); }
		
		var str:String = "{";
		
		for ( var p:String in target ) {
			str += p + ":";
			
			var value:* = target[p];
			
			switch ( true ) {
				case value is Array		: { str += ArrayUtil.toString( value ); break; }
				case value is Boolean	:
				case value is Number	:
				case value is int		:
				case value is uint		: { str += value; break; }
				case value is String	: { str += '"' + value.replace( new RegExp( '"', "gi" ), '\\"' ) + '"'; break; }
				default					: { str += toString( value ); }
			}
			
			str += ", ";
		}
		
		// 1 度でもループを処理していれば最後の , を削除する
		if ( p ) {
			str = str.slice( 0, -2 );
		}
		
		str += "}";
		
		return str;
	}
}


class ArrayUtil{
	
	/**
	 * <span lang="ja">指定された配列のストリング表現を返します。</span>
	 * <span lang="en">Returns the string expression of the specified array.</span>
	 * 
	 * @param array
	 * <span lang="ja">対象の配列です。</span>
	 * <span lang="en">The array to process.</span>
	 * @return
	 * <span lang="ja">配列のストリング表現です。</span>
	 * <span lang="en">The string expression of the array.</span>
	 * 
	 * @example <listing version="3.0">
	 * </listing>
	 */
	public static function toString( array:Array ):String {
		var str:String = "[";
		
		for ( var i:int = 0, l:int = array.length; i < l; i++ ) {
			var item:* = array[i];
			
			switch ( true ) {
				case item is Array		: { str += ArrayUtil.toString( item ); break; }
				case item is Boolean	:
				case item is Number		:
				case item is int		:
				case item is uint		: { str += item; break; }
				case item is String		: { str += '"' + item.replace( new RegExp( '"', "gi" ), '\\"' ) + '"'; break; }
				default					: { str += ObjectUtil.toString( item ); }
			}
			
			str += ", ";
		}
		
		// 1 度でもループを処理していれば最後の , を削除する
		if ( i > 0 ) {
			str = str.slice( 0, -2 );
		}
		
		str += "]";
		
		return str;
	}	
	
	
}